﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace h1
{
    internal class MySqlHelper
    {
        public static string Conn = "server=localhost;port=3306;user=root;password=1234; database=homework1;";
        public static DataSet getDataSet(string connectionString, string cmdText)
        {
            DataSet ds = new DataSet();
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();
                        using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(cmdText, conn))
                        {
                            myAdapter.Fill(ds);
                        }
                    }
                    catch (Exception ex)
                    {
                        Form_message fm = new Form_message("数据库数据读取出错", "出错描述：" + ex.Message);
                        fm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("数据库数据连接出错", "出错描述：" + ex.Message);
            }
            return ds;
        }
        public static void updateDataSet(string connectionString, string cmdText,DataSet ds)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();
                        using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(cmdText, conn))
                        {
                            MySqlCommandBuilder builder = new MySqlCommandBuilder(myAdapter);
                            myAdapter.Update(ds);
                        }
                    }
                    catch (Exception ex)
                    {
                        Form_message fm = new Form_message("数据库数据更新出错", "出错描述：" + ex.Message);
                        fm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("数据库数据连接出错", "出错描述：" + ex.Message);
            }
        }
        public static void updateDataSet(string connectionString, string cmdText, DataTable ds)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();
                        using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(cmdText, conn))
                        {
                            MySqlCommandBuilder builder = new MySqlCommandBuilder(myAdapter);
                            myAdapter.Update(ds);
                        }
                    }
                    catch (Exception ex)
                    {
                        Form_message fm = new Form_message("数据库数据更新出错", "出错描述：" + ex.Message);
                        fm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("数据库数据连接出错", "出错描述：" + ex.Message);
            }
        }
    }
}